package it.uniroma3.dia.digraph.view;

import it.uniroma3.dia.digraph.algorithm.Algorithm;
import it.uniroma3.dia.digraph.algorithm.kouterplanar.kouterplanar.IDMAlgo;
import it.uniroma3.dia.digraph.algorithm.kouterplanar.kouterplanar.Prova;
import it.uniroma3.dia.digraph.algorithm.kouterplanar.outerplanar.MakeConvex;
import it.uniroma3.dia.digraph.algorithm.path.LinearizePath;
import it.uniroma3.dia.digraph.algorithm.stackedTri.ConvexCoordMorphing;
import it.uniroma3.dia.digraph.algorithm.utility.stackedtri.StackedTriGraph;
import it.uniroma3.dia.digraph.algorithm.utility.stackedtri.barycentric.CombinationGraph;
import it.uniroma3.dia.digraph.io.util.IoManager;
import it.uniroma3.dia.digraph.view.interpolation.impl.BarCoordsInterpolator;
import it.uniroma3.dia.digraph.view.interpolation.impl.LinearInterpolator;

import java.io.IOException;
import java.util.Scanner;

import org.graphdrawing.graphml.xmlns.GraphType;
import org.graphdrawing.graphml.xmlns.GraphmlType;


public class Finestrella {
	private static Scanner scanner;
	
	public static void getChar(){
		if (scanner==null)
			scanner = new Scanner(System.in);
		scanner.nextLine();
	}
	public static void main(String[] args) throws IOException{
		IoManager iom = new IoManager();
		
//		path(1, iom);
//		path(2, iom);
//		
//		stackedTri(iom);
//		
//		makeConvex(1, iom);
//		
//		makeConvex(2, iom);
//		stackedInterpolation(1);
//		stackedInterpolation(2);
//		stackedInterpolation(3);
//		linearIntrerpolation(2,iom);
		 
//		outerplanar(0,iom);
//		outerplanar(1,iom);
//		outerplanar(2,iom);
//		outerplanar(3,iom);
//		outerplanar(4,iom);
//		outerplanar(5,iom);
//		outerplanar(6,iom);
//		outerplanar(7,iom);
		prova(1,iom);
		prova(2,iom);
		prova(3,iom);
		prova(4,iom);
		prova(5,iom);
		prova(6,iom);		
		prova(7,iom);
		System.exit(0);
		
	}
	
	/**
	 * @param i
	 * @param iom
	 * @throws IOException 
	 */
	private static void outerplanar(int i, IoManager iom) throws IOException {
	    GraphmlType a, b;
	    a = null;
	    b= null;
	    if (i==0) {
		a = iom.loadFromFile("xml\\kouterplanar\\prova\\source.xml");
		b = iom.loadFromFile("xml\\kouterplanar\\prova\\target.xml");
	    }
	    if (i==1) {
		a = iom.loadFromFile("xml\\kouterplanar\\kouterpl\\yed\\sourceG.xml");
		b = iom.loadFromFile("xml\\kouterplanar\\kouterpl\\yed\\targetG.xml");		
	    }
	    if(i==2) {
		a = iom.loadFromFile("xml\\kouterplanar\\fantasy\\bohT.xml");
		b = iom.loadFromFile("xml\\kouterplanar\\fantasy\\bohS.xml");
	    }
	    if(i==3) {
		a = iom.loadFromFile("xml\\kouterplanar\\prova\\figure.xml");
		b = iom.loadFromFile("xml\\kouterplanar\\prova\\star.xml");
	    }
	    if(i==4) {
		a = iom.loadFromFile("xml\\kouterplanar\\prova\\buh.xml");
		b = iom.loadFromFile("xml\\kouterplanar\\prova\\esse.xml");
	    }
	    if(i==5) {
		a = iom.loadFromFile("xml\\kouterplanar\\prova\\starConvex.xml");
		b = iom.loadFromFile("xml\\kouterplanar\\prova\\star.xml");
	    }
	    if(i==6) {
		a = iom.loadFromFile("xml\\kouterplanar\\fantasy\\bohS.xml");
		b = iom.loadFromFile("xml\\kouterplanar\\fantasy\\bohZ.xml");
	    }
	    if(i==7) {
		b = iom.loadFromFile("xml\\kouterplanar\\fantasy\\bohT.xml");
		a = iom.loadFromFile("xml\\kouterplanar\\fantasy\\bohZ.xml");
	    }
	    
	    run(IDMAlgo.class, a.getGraph(),b.getGraph());
	}
	/**
	 * @param i
	 * @param iom
	 * @throws IOException 
	 */
	private static void prova(int i, IoManager iom) throws IOException {
	    GraphmlType a, b,c;
	    a = null;
	    b= null;
	    c=null;
	    if (i==0) {
		a = iom.loadFromFile("xml\\kouterplanar\\prova\\source.xml");
		b = iom.loadFromFile("xml\\kouterplanar\\prova\\target.xml");
		c = iom.loadFromFile("xml\\kouterplanar\\prova\\target.xml");
	    }
	    if (i==1) {
		a = iom.loadFromFile("xml\\kouterplanar\\kouterpl\\yed\\sourceG.xml");
		b = iom.loadFromFile("xml\\kouterplanar\\kouterpl\\yed\\targetG.xml");
		c = iom.loadFromFile("xml\\kouterplanar\\kouterpl\\yed\\targetG.xml");
	    }
	    if(i==2) {
		a = iom.loadFromFile("xml\\kouterplanar\\fantasy\\bohT.xml");
		b = iom.loadFromFile("xml\\kouterplanar\\fantasy\\bohS.xml");
		c = iom.loadFromFile("xml\\kouterplanar\\fantasy\\bohS.xml");
	    }
	    if(i==3) {
		a = iom.loadFromFile("xml\\kouterplanar\\prova\\figure.xml");
		b = iom.loadFromFile("xml\\kouterplanar\\prova\\star.xml");
		c = iom.loadFromFile("xml\\kouterplanar\\prova\\star.xml");
	    }
	    if(i==4) {
		a = iom.loadFromFile("xml\\kouterplanar\\prova\\buh.xml");
		b = iom.loadFromFile("xml\\kouterplanar\\prova\\esse.xml");
		c = iom.loadFromFile("xml\\kouterplanar\\prova\\esse.xml");
	    }
	    if(i==5) {
		a = iom.loadFromFile("xml\\kouterplanar\\prova\\starConvex.xml");
		b = iom.loadFromFile("xml\\kouterplanar\\prova\\star.xml");
		c = iom.loadFromFile("xml\\kouterplanar\\prova\\star.xml");
	    }
	    if(i==6) {
		a = iom.loadFromFile("xml\\kouterplanar\\fantasy\\bohS.xml");
		b = iom.loadFromFile("xml\\kouterplanar\\fantasy\\bohZ.xml");
		c = iom.loadFromFile("xml\\kouterplanar\\fantasy\\bohZ.xml");
	    }
	    if(i==7) {
		c = iom.loadFromFile("xml\\kouterplanar\\fantasy\\bohT.xml");
		b = iom.loadFromFile("xml\\kouterplanar\\fantasy\\bohT.xml");
		a = iom.loadFromFile("xml\\kouterplanar\\fantasy\\bohZ.xml");
	    }
	    
	    run(Prova.class, a.getGraph(),b.getGraph(), c.getGraph());
	}
	
	
	/**
	 * @param i
	 * @throws IOException 
	 */
	private static void linearIntrerpolation(int i, IoManager iom) throws IOException {
	    GraphmlType a, b;
	    a = null;
	    b= null;
	    if (i==1) {
		a = iom.loadFromFile("xml\\stackedTri\\ciccio\\stc1.graphml");
		b = iom.loadFromFile("xml\\stackedTri\\ciccio\\stc2.graphml");
	    }
	    if (i==2) {
		a = iom.loadFromFile("xml\\stackedTri\\fuori\\fuori1.xml");
		b = iom.loadFromFile("xml\\stackedTri\\fuori\\fuori2.xml");
//		c = iom.loadFromFile("xml\\stackedTri\\fuori\\fuori1.xml");
	    }
	    
	   	    
	    View v = new View("stackedTri", 800, 600, a.getGraph());
	    
//	    System.out.println(gg.getNodeMap().get("d").getParent2lambda());
	    
	    
	    
	    BarCoordsInterpolator bci = new BarCoordsInterpolator(v.getDrawingPanel());
	    v.addInterpolator(bci);
	    LinearInterpolator lin = new LinearInterpolator(v.getDrawingPanel());
////	    v.addInterpolator(lin);
//	    bci.setCombinationGraph(gg);
	    
	    v.pack();
	    v.setVisible(true);
	    System.out.println("press enter to start");
	    getChar();
	    lin.interpolate(b.getGraph());
//	    v.update(b.getGraph());
//	    v.parallelUpdate(a.getGraph(),c.getGraph());
//	    lin.interpolate(b.getGraph());
	    
	    v.repaint();
	    getChar();
	    lin.interpolate(a.getGraph());
	    
 
	}
	private static void path(int exampleNum, IoManager iom) throws IOException{
		GraphType g = null;
		
		if (exampleNum==1)
			g = iom.loadFromFile("xml\\path\\camminoTriangoli.graphml").getGraph();
		
		if (exampleNum==2)
			g = iom.loadFromFile("xml\\path\\doppiaSpira.graphml").getGraph();
		
		run(LinearizePath.class, g);
	}
	
	/**
	 * runs a demo for stacked triangulation morphing
	 * @param iom
	 * @throws IOException
	 */
	private static void stackedTri(IoManager iom) throws IOException{
		GraphType a = iom.loadFromFile("xml\\stackedTri\\fuori\\fuori1.xml").getGraph();
		GraphType b = iom.loadFromFile("xml\\stackedTri\\fuori\\fuori2.xml").getGraph();
		
		View v = new View(a);
		v.pack();
		v.setVisible(true);
		v.repaint();
		v.update(a);
		getChar();
		v.update(b);
		v.repaint();
		
		
//		run(ConvexCoordMorphing.class , a, b);
	}
	
	/**
	 * runs a demo making convex an outerplanar graph
	 * @param paramNo 
	 * @param iom
	 * @throws IOException
	 */
	private static void makeConvex(int paramNo, IoManager iom) throws IOException{
		GraphType gm = null;
		if (paramNo==1)
		    gm = iom.loadFromFile("xml\\kouterplanar\\simpleOuterplanar.xml").getGraph();
		else
		    gm = iom.loadFromFile("xml\\kouterplanar\\bentOuterplanar.xml").getGraph();
		
		run(MakeConvex.class, gm);
		
	}
	



	
	private static void run (Class<? extends Algorithm> c, GraphType... input){
		try {
			System.out.println("press enter to start");
			getChar();
//			Algorithm a = (Algorithm) Class.forName(c.getName()).newInstance();
			Algorithm a = c.newInstance();			
			a.run(input);
			System.out.println("press enter to close frame");
			new Scanner(System.in).nextLine();
			a.getView().setVisible(false);
		} catch (Exception e) {			
			e.printStackTrace();
		}
	}
/**
 * @param i
 * @throws IOException
 */
//	/**
//	 * @throws IOException
//	 */
//	private static void doppiaspira() throws IOException {
//		IoManager iom = new IoManager();
//		GraphmlType gml = iom.loadFromFile("xml\\path\\doppiaSpira.graphml");
//		View v = new View("path", 800,600, gml.getGraph());		
//		v.pack();
//		v.setVisible(true);
//		v.update(gml.getGraph());
//		v.repaint();
//		System.out.println("press enter to start");
//		getChar();
//		Algorithm algo = new LinearizePath();
//		algo.setView(v);
//		algo.apply(gml.getGraph());
//		System.out.println("press enter");
//		new Scanner(System.in).nextLine();
//		v.setVisible(false);
//	}
//	
//	private static void camminoTriangolo() throws IOException {
//		IoManager iom = new IoManager();
//		GraphmlType gml = iom.loadFromFile("xml\\path\\camminoTriangoli.graphml");
//		View v = new View("path", 800,600, gml.getGraph());		
//		v.pack();
//		v.setVisible(true);
//		v.update(gml.getGraph());
//		v.repaint();
//		System.out.println("press enter to start");
//		getChar();
//		Algorithm algo = new LinearizePath();
//		algo.setView(v);
//		algo.apply(gml.getGraph());
//		System.out.println("press enter");
//		new Scanner(System.in).nextLine();
//		v.setVisible(false);
//	}
//
//	private static void stackedTri() throws IOException {
//		IoManager iom = new IoManager();
//		GraphmlType a = iom.loadFromFile("xml\\stackedTri\\ciccio\\stc1.graphml");
//		GraphmlType b = iom.loadFromFile("xml\\stackedTri\\ciccio\\stc2.graphml");
//		View v = new View("stackedTri", 800, 600, a.getGraph());
//		v.pack();
//		v.setVisible(true);
//		v.update(a.getGraph());
//		v.repaint();
//		System.out.println("press enter to start");
//		getChar();
//		Algorithm stackedTriBaryc = new ConvexCoordMorphing();
//		stackedTriBaryc.setView(v);
//		stackedTriBaryc.run( a.getGraph(), b.getGraph());
//		System.out.println("press enter");
//		new Scanner(System.in).nextLine();
//		v.setVisible(false);
//		
//	}

	public static void stackedInterpolation(int i) throws IOException {
	    IoManager iom = new IoManager();
	    GraphmlType a = null;
	    GraphmlType b = null;
//	    GraphmlType c = null;
	    if (i==1) {
		a = iom.loadFromFile("xml\\stackedTri\\ciccio\\stc1.graphml");
		b = iom.loadFromFile("xml\\stackedTri\\ciccio\\stc2.graphml");
	    }
	    if (i==2) {
		a = iom.loadFromFile("xml\\stackedTri\\fuori\\fuori1.xml");
		b = iom.loadFromFile("xml\\stackedTri\\fuori\\fuori2.xml");
//		c = iom.loadFromFile("xml\\stackedTri\\fuori\\fuori1.xml");
	    }
	    if (i==3) {
		a = iom.loadFromFile("xml\\stackedTri\\blah\\triS.xml");
		b = iom.loadFromFile("xml\\stackedTri\\blah\\triT.xml");
	    }

	    StackedTriGraph aa = new StackedTriGraph(a.getGraph());
	    StackedTriGraph bb = new StackedTriGraph(b.getGraph());
	    CombinationGraph gg = new CombinationGraph(aa, bb);	    
	    View v = new View("stackedTri", 800, 600, a.getGraph());
	    
//	    System.out.println(gg.getNodeMap().get("d").getParent2lambda());
	    
	    
	    
	    BarCoordsInterpolator bci = new BarCoordsInterpolator(v.getDrawingPanel());
	    v.addInterpolator(bci);
//	    LinearInterpolator lin = new LinearInterpolator(v.getDrawingPanel());
//	    v.addInterpolator(lin);
	    bci.setCombinationGraph(gg);
	    
	    v.pack();
	    v.setVisible(true);
	    System.out.println("press enter to start");
	    getChar();
	    v.update(a.getGraph());
//	    v.parallelUpdate(a.getGraph(),c.getGraph());
//	    lin.interpolate(b.getGraph());
	    
	    v.repaint();
	    getChar();
	    

	}

	
	
	
}
